﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ttifa.Infrastructure.Utils
{
    public class DateTimeHelper
    {
        /// <summary>
        /// datetime 转成Unix时间戳
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>

        public static long GetUnixTimeStamp(DateTime time)
        {
            DateTime unixStartTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            TimeSpan timeSpan = time.Subtract(unixStartTime);

            return timeSpan.Ticks / 10000000;
        }

        /// <summary>
        /// 时间戳转为C#格式时间
        /// </summary>
        /// <param name="lTime"></param>
        /// <returns></returns>
        public static DateTime GetTimeByTimeStamp(long lTime)
        {
            DateTime timeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            TimeSpan toNow = new TimeSpan(long.Parse($"{lTime.ToString()}0000000"));

            return timeStart.Add(toNow);
        }
    }
}
